package com.tongcao.cn.contoller;

import com.google.common.collect.Maps;
import com.tongcao.cn.dfire.result.ResultMap;
import com.tongcao.cn.dfire.utils.ApiResultUtil;
import com.tongcao.cn.domain.dataobject.User;
import com.tongcao.cn.service.IUserService;
import com.tongcao.cn.service.UidService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author:tongcao
 * @Date:2019/1/2 15:37
 * Description:构建rest API
 */
@RestController
@RequestMapping("/users")
public class UserRestApiController {

    static Map<Long, com.tongcao.cn.domain.entity.User> users = Collections.synchronizedMap(Maps.newHashMap());


    @Resource
    private IUserService userService;

    @Resource
    private UidService uidService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<com.tongcao.cn.domain.entity.User> getUserList() {
        // 处理"/users/"的GET请求，用来获取用户列表
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        List<com.tongcao.cn.domain.entity.User> r = new ArrayList<com.tongcao.cn.domain.entity.User>(users.values());
        return r;

    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postUser(@ModelAttribute com.tongcao.cn.domain.entity.User user) {
        // 处理"/users/"的POST请求，用来创建User
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        users.put(user.getId(), user);
        return "success";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public com.tongcao.cn.domain.entity.User getUser(@PathVariable Long id) {
        // 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        return users.get(id);
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @ModelAttribute com.tongcao.cn.domain.entity.User user) {
        // 处理"/users/{id}"的PUT请求，用来更新User信息
        com.tongcao.cn.domain.entity.User u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return "success";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        // 处理"/users/{id}"的DELETE请求，用来删除User
        users.remove(id);
        return "success";
    }

    @RequestMapping(value = "/getAllUser", method = RequestMethod.GET)
    public ResultMap getUserLists() {
        // 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        return ApiResultUtil.successResult(userService.getUserList());
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.GET)
    public ResultMap addOne() {

        User user = new User();
        user.setName("booker");
        user.setGender(1);
        user.setAge(20);
        user.setEmail("503201142@qq.com");
        user.setPhone("18373187726");
        user.setAvatar("dsa");
        user.setBirthday(0);
        user.setId(uidService.genId());
        user.setCreateTime(System.currentTimeMillis());
        user.setOpTime(System.currentTimeMillis());
        user.setValid(1);
        userService.insert(user);
        return ApiResultUtil.successResult(true);
    }


}
